<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
        }
        .a{
            width: 30px;
            height: 30px;
            background-color: green;
            position: absolute;
            border-radius: 50%;
        }

        .dot{
            width: 30px;
            height: 30px;
            background-color: blue ;
            position: absolute;
            border-radius: 50%;
        }
    
    </style>
</head>
<body>
    <div class="a" style="background:red;z-index:3"></div>
    <div class="a" ></div>
    <div class="a"></div>
    <div class="a"></div>
    <div class="a"></div>
    <div class="a"></div>
    <div class="a"></div>
    <div class="a"></div>
    <div class="a"></div>
    <div class="a"></div>


    <!-- <div class="dot"></div> -->

    <script>

        var oBox = document.querySelectorAll('.a') ;
        var t ;
        var maxX = document.documentElement.clientWidth - oBox[0].offsetWidth ;
        var maxY = document.documentElement.clientHeight - oBox[0].offsetHeight ;

        addDot()
        // 判断有没有吃到
        var oDot = document.querySelector('.dot') ;


        document.onkeyup = function (e) {  
            e = e || event ; 
            var code = e.keyCode || e.which ;
            console.log(code);  // 上38 下40  左37 右39
            var speed = 30 ;
            clearInterval(t)
            t = setInterval(function () {  
                var x = parseInt(getComputedStyle(oBox[0]).left) ;
                var y = parseInt(getComputedStyle(oBox[0]).top) ;
                if(code === 37) x -= speed ;
                if(code === 38) y -= speed ;
                if(code === 39) x += speed ;
                if(code === 40) y += speed ;
                if(x > maxX) code = 37 ;
                if(y > maxY) code = 38 ;
                if(x < 0) code = 39;
                if(y < 0) code = 40 ;
                oBox[0].style.cssText += `left:${x}px;top:${y}px` ;
                for(var i = oBox.length - 1 ; i > 0 ; i--) {
                    oBox[i].style.left = getComputedStyle(oBox[i- 1]).left ;
                    oBox[i].style.top = getComputedStyle(oBox[i- 1]).top ;
                }


                // 判断有没有吃到
                if(oBox[0].style.left === oDot.style.left && oBox[0].style.top === oDot.style.top) {
                    // console.log('吃到了');
                    oDot.remove() ;   // 删除食物
                    addDiv();   // 尾巴增加一个
                    addDot()   // 再创建一个食物
                    oBox = document.querySelectorAll('.a') ;
                    oDot = document.querySelector('.dot') ;
                    console.log(oBox);
                }


            },100)
        }






        // 尾巴边长了
        function addDiv() {  
            var oDiv = document.createElement('div') ;
            oDiv.className = 'a' ;
            var x = oBox[oBox.length - 1].offsetLeft ;
            var y = oBox[oBox.length - 1].offsetTop ;
            oDiv.style.left = x + 'px';
            oDiv.style.top = y + 'px';
            document.body.appendChild(oDiv);
        }



        function addDot() {  
            var oDiv = document.createElement('div') ;
            oDiv.className = 'dot' ;
            var x = rand(0 , parseInt(maxX / 30)) * 30 ;
            var y = rand(0 , parseInt(maxY / 30)) * 30 ;
            oDiv.style.cssText = `left:${x}px;top:${y}px` ;
            console.log(oDiv);
            document.body.appendChild(oDiv)
        }


        function rand(min , max) { 
            return parseInt(Math.random() * (max - min) + min)
         }

    </script>
    
</body>
</html>